package com.mes.inventory.dao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import com.bstek.dorado.data.provider.Page;
import com.mes.common.dao.BasicDao;
import com.mes.inventory.dao.iface.IInvCheckDao;
import com.mes.inventory.domain.InvCheck;
@Repository
public class InvCheckDao extends BasicDao<InvCheck> implements IInvCheckDao{

	@Override
	public void getInvCheck(Page<InvCheck> page, Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
				Map<String, Object> args = new HashMap<String, Object>();
				StringBuffer coreHql = new StringBuffer("from "
						+ InvCheck.class.getName() + " a where 1=1 ");

				String warehouseUid = (String) params.get("warehouseUid");
				if (StringUtils.isNotEmpty(warehouseUid)) {
					coreHql.append(" and a.warehouseUid =:warehouseUid ");
					args.put("warehouseUid", warehouseUid);
				}
				Integer invCheckState = (Integer) params.get("invCheckState");
				if (null != invCheckState) {
					coreHql.append(" and a.invCheckState =:invCheckState ");
					args.put("invCheckState", invCheckState);
				}
				
				String countHql = "select count(*) " + coreHql.toString();
				String hql = coreHql.toString();
				
				Map<String, Object> newArgs = new HashMap<String, Object>();
				String key;
				String newKey;
				Iterator<String> iterator = args.keySet().iterator();
				while(iterator.hasNext()){
					key = (String)iterator.next();
					newKey = key.replace(".", "_");
					newArgs.put(newKey, args.get(key));
					hql = hql.replace(":"+key, ":"+newKey);
					countHql = countHql.replace(":"+key, ":"+newKey);
					
					System.out.println("hql: " + hql);
					System.out.println("countHql: " + countHql);
					System.out.println("newArgs: " + newArgs);
				}
				this.pagingQuery(page, hql, countHql, newArgs);
			}	
}
